package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import com.itheima.health.utils.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @ClassName PermissionServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2020/2/13 16:03
 * @Version V1.0
 */
@Service // dubbo提供
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        //初始化数据
        PageHelper.startPage(currentPage,pageSize);
        //查询返回page
        Page<Permission> page = null;
        Jedis jedis = null;
        String permission_data = null;
        long totalCount=0L;
        String total=null;
        List<Permission> permissionList=null;
        try {
            //0. 从Redis获得,
            jedis = JedisUtils.getJedis();
            if(jedis != null){
                total=jedis.get("total");
                permission_data = jedis.get("permission_data");
                if (total!=null&&permission_data!=null){
                    totalCount=Long.parseLong(total);
                    permissionList =  JSON.parseObject(permission_data,List.class);
                }
                if (totalCount!=0&&permissionList != null){
                    //1.1 有就直接响应
                    System.out.println("Redis里面有, 从Redis里面获得...");
                } else{
                    System.out.println("Redis里面没有, 从Mysql获得, 再存到Redis...");
                    //1.2 没有从Mysql获得
                    page = permissionDao.selectByCondition(queryString);
                    totalCount = page.getTotal();
                    permissionList = page.getResult();

                    //1.3 再存到Redis
                    total=JSON.toJSONString(totalCount);
                    jedis.set("total",total);
                    permission_data = JSON.toJSONString(permissionList);
                    jedis.set("permission_data",permission_data);

                }
            }

        } catch (Exception e) {
            e.printStackTrace();

        }finally {

            //JedisUtils.close(jedis);
            return new PageResult(totalCount,permissionList);
        }


    }

    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public Permission findById(Integer id) {
        Permission permission=permissionDao.findPermissionsById(id);
        return permission;
    }

    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public void delete(Integer id) {
        //查询当前权限是否和角色关联
        long count = permissionDao.findCountByPermissionId(id);
        if(count > 0){
            //当前权限被引用，不能删除
            throw new RuntimeException("当前权限被角色引用，不能删除");
        }
        permissionDao.deleteById(id);
    }

    @Override
    public List<Permission> findAll() {
        List<Permission> list = permissionDao.findAll();
        return list;
    }
}
